<template>
  <d2-container>
    <div class="row-ac pb-2">
      <div><el-button size="small" type="warning" @click="addTask" icon="el-icon-plus">添加任务</el-button></div>
      <div class="ml-2"><el-button size="small" type="danger" icon="el-icon-delete">批量删除</el-button></div>
    </div>
    <el-table :data="taskList" border stripe>
      <el-table-column align="center" type="selection" width="55" />
      <el-table-column align="center" label="序号" type="index" width="65" />
      <el-table-column align="center" label="任务名称" prop="title" width="160" />
      <el-table-column align="center" label="任务价格" prop="task_price" width="95" />
      <el-table-column align="center" label="任务金币" prop="task_gold" width="95" />
      <el-table-column align="center" label="人均用时(分钟)" prop="usage_time" width="95" />
      <el-table-column align="center" label="审核时间(分钟)" prop="audit_time" width="65" />
      <el-table-column align="center" label="份数" prop="total_copies" width="55" />
      <el-table-column align="center" label="已完成" prop="completed_copies" width="55" />
      <el-table-column align="center" label="操作" width="180">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)">修改</el-button>
          <el-button size="mini" type="danger" @click="openDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="flex-1 mt-3">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page.sync="queryInfo.page" layout="total, prev, pager, next" :total="total">
      </el-pagination>
    </div>
  </d2-container>
</template>

<script>
  import {
    taskListUrl,
    taskDetailUrl,
    taskUpdateUrl,
    taskDeleteUrl,
    taskCreateUrl
  } from '@/utils/task'
  export default {
    name: 'category',
    data() {
      return {
        taskcategoryList: [],
        taskList:[],
        form: {
          name: '',
          sort: 1,
          parent: '',
          icon_font: '',
          cover_image: ''
        },
        props: {
          value: 'id',
          label: 'name',
        },
        rules: {
          name: [{
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          }]
        },
        getCategoryId: '',
        queryInfo: {
          page: 1,
          pageSize: 10
        },
        total: 0
      }
    },
    created() {
      this.initData()
    },
    methods: {
      initData() {
        this.getTaskList()
      },
      getTaskList() {
        this.$get(taskListUrl, this.queryInfo).then(res => {
          this.taskList = res.data.data.list
          this.total = res.data.data.total
        })
      },
      // 分页页码显示条数
      handleSizeChange(newSize) {
        // 把每页显示多少数据重新赋值
        this.queryInfo.pageSize = newSize
        this.getTaskList()
      },
      // 翻页事件
      handleCurrentChange(newPage) {
        this.queryInfo.page = newPage
        this.getTaskList()
      },
      openDelete(taskId) {
        this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 在确定时执行handleDelete的删除
          this.handleDelete(taskId);
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      handleDelete(taskId) {
        this.$del(taskDeleteUrl + taskId + '/').then(res => {
          this.getTaskList()
        })
      },
      addTask(){
        this.$router.push('/task/addtask');
      },
      handleEdit(taskId){
        this.$router.push({
          path: '/task/addtask',
          query: {
            taskId: taskId
          }
        })
      }
    }
  }
</script>
